<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>

<body>
	<script>
		var user = {}
		var age;
		Object.defineProperty(user, 'age', {
			get: function () {
				console.log("获取年龄");
				return age;
			},
			set: function (newVal) {
				console.log("设置年龄");
				age = newVal;
			}
		});
		user.age = 18;
		console.log(user.age);
	</script>
</body>

</html>